﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.ImageFormats
{
    /// <summary>
    /// Tiff
    /// </summary>
    public class Tiff : AbstractImageFormat
    {
        private static int _PageNumber = -1;


        /// <summary>
        /// Creates the specified _s file name.
        /// </summary>
        /// <param name="_sFileName">Name of the _s file.</param>
        /// <returns></returns>
        public static UnsafeBitmap Create(string _sFileName)
        {
            return SplitTiffImage(Image.FromFile(_sFileName));
        }

        /// <summary>
        /// Splits the tiff image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns></returns>
        public static UnsafeBitmap SplitTiffImage(Image image)
        {
            Guid objGuid = image.FrameDimensionsList[0];
            FrameDimension objDimension = new FrameDimension(objGuid);
            image.SelectActiveFrame(objDimension, 0);

            //Saves every frame as a separate file.
            Encoder enc = Encoder.Compression;
            int curFrame = 0;
            for (int i = 0; i < _PageNumber; i++)
            {
                image.SelectActiveFrame(objDimension, curFrame);
                EncoderParameters ep = new EncoderParameters(1);
                //ep.Param[0] = new EncoderParameter(enc, (long)format);
                ImageCodecInfo info = GetEncoderInfo("image/tiff");

                curFrame++;
            }

            return new UnsafeBitmap(image);
        }

        /// <summary>
        /// Gets encoder info
        /// </summary>
        /// <param name="mimeType"></param>
        /// <returns></returns>
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        /// <summary>
        /// Read the image file for the page number.
        /// </summary>
        private void GetPageNumber()
        {
            //Guid objGuid = image.FrameDimensionsList[0];
            //FrameDimension objDimension = new FrameDimension(objGuid);

            ////Gets the total number of frames in the .tiff file
            //_PageNumber = image.GetFrameCount(objDimension);
        }

        /// <summary>
        /// returns Extension
        /// </summary>
        public override string Extension
        {
            get { return "tif"; }
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <param name="_sFileName"></param>
        /// <returns></returns>
        public override bool Save(string _sFileName)
        {
            throw new NotImplementedException();
        }
    }
}
